<?PHP
/**
 * validation.php - A collection of validation functions
 *
 * @category   babel
 * @package    BABEL
 * @author     Magnus Jonsson <magnus.jonsson@codealliance.se>
 * @copyright  2008 Team babel
 * @license    LGPL
 */


/**
 * Check if a date string is a valid date.
 *
 * A valid date format is an ISO date. Ex: 2005-03-01
 *
 * @access public
 * @param string Date
 */

function isValidDate($date)
{
    if( preg_match("/^(\d{4})\-(\d{2})\-(\d{2})$/", $date, $match ) == 0 ) {
        return false;
    }

    if( checkdate($match[2], $match[3], $match[1] ) != true ) {
        return false;
    }
    return true;
}

/**
 * Check if a phone number is valid
 *
 * A valid phone number can only contain numbers and these characters: +-()
 *
 * @access public
 * @param string Phone Number
 */
function isValidPhoneNumber($number, $country=false) {
	$len = strlen($number);
	if ($len < 4 || $len > 20) {
		return false;
	}

	return preg_match('/^\+?[0-9]+(\s?[\-\(]?\s?[0-9]+\)?)*$/', $number);
}
/**
 * Validate Email Address
 *
 * @access public
 * @param string Email string
 * @param boolean Check DNS
 * @return boolean TRUE if Valid, FALSE if Invalid
 */
function isValidEmail($email){

	return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

/**
 * Validate IP Address
 *
 * @access public
 * @param string IP Address
 * @return boolean TRUE if Valid, FALSE if Invalid
 */
function isValidIPAddress($ip_addr) {
	$long = ip2long($ip_addr);
	return $long ? long2ip($long) == $ip_addr : false;
}

function isValidChecksum($md5){
	if (!ctype_alnum($md5) || strlen($md5)!=32) {
		return false;
	} else {
		return true;
	}
}

function isValidUsername($username){
	$len = strlen($username);
	return preg_match('/^[a-zA-Z0-9]+([-_]?[a-zA-Z0-9]+)*$/', $username) &&
		$len < 33 && $len > 2;
}

function isValidUid($uid) {
	if(!is_numeric($uid) || $uid < 0) {
		return false;
	} else {
		return true;
	}
}

function isValidName($string, $max_len=20, $min_len=0){
	if (preg_match('/[a-zA-Z\pLetter]{' . $min_len . ',' . $max_len . '}$/u', $string)) {
		return true;
    }
    return false;
}

function isValidAsciiString($string, $max_len=20, $min_len=0){
	$len = strlen($string);
	if ($min_len > 0 && $len<$min_len) {
		return false;
	}

	if ($max_len > 0 && $len>$max_len) {
		return false;
	}

    preg_match_all('/[0-9a-zA-Z\s]/',$string,$tmp_holder);
    $tmp_holder=implode($tmp_holder[0]);
    if ($tmp_holder!=$string) {
		return false;
    }

    return true;
}

function isValidUnicodeString($string, $max_len=20, $min_len=0){
	$len = strlen($string);
	if ($min_len > 0 && $len<$min_len) {
		return false;
	}

	if ($max_len > 0 && $len>$max_len) {
		return false;
	}

    preg_match_all('/[\p{L}\p{Nd}\s]/',$string,$tmp_holder);
    $tmp_holder=implode($tmp_holder[0]);
    if ($tmp_holder!=$string) {
		return false;
    }

    return true;
}

function isValidCountry($country_code){
	if(strlen($GLOBALS['countries'][$country_code])>0){
		return true;
	}
	return false;
}

function isValidCategory($relationship, $allowCombos = false, $allowPrivate = false){
	if( $allowPrivate && $relationship == _CT_PRIVATE)
	{
		return true;
	}
	if( $allowCombos ){
		if( $relationship & _CT_ALLOWED == $relationship ){
			return true;
		}else{
			return false;
		}
	}else{
		switch($relationship){
			case _CT_FAMILY:
			case _CT_FRIENDS:
			case _CT_BUSINESS:
			case _CT_OTHER:
			case _CT_PUBLIC:
				return true;
			default:
				return false;
		}
	}
}
?>
